package com.css.product.controller;

import com.css.product.VO.ResultVO;
import com.css.product.dataobject.ProductInfo;
import com.css.product.dto.CartDTO;
import com.css.product.repository.ProductInfoRepository;
import com.css.product.service.ProductService;
import com.css.product.util.ResultVOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("product")
public class ProductController {

    @Autowired
    ProductInfoRepository productInfoRepository;
    @Autowired
    private ProductService productService;

    @RequestMapping("list")
    public ResultVO<ProductInfo> list() {
        List<ProductInfo> byProductStatus = productInfoRepository.findByProductStatus(1);
        return ResultVOUtil.success(byProductStatus);
    }

    @PostMapping("/listForOrder")
    public List<ProductInfo> listForOrder(@RequestBody List<String> productIdList) {
        return productInfoRepository.findByProductIdIn(productIdList);
    }

    @PostMapping("/decreaseStock")
    public ResultVO decreaseStock(@RequestBody List<CartDTO> cartDTOS) {
        productService.decreaseStock(cartDTOS);
        return ResultVOUtil.success(null);
    }

}
